package io.kiki.sba.registry.server.metadata.slot.manager;

import io.kiki.sba.registry.common.model.slot.Slots;
import io.kiki.sba.registry.observer.Observable;
import io.kiki.sba.registry.observer.UnblockingObserver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final class SlotsChangeNotification implements UnblockingObserver {
    private static final Logger logger = LoggerFactory.getLogger(SlotsChangeNotification.class);
    private final DefaultSlotManagerImpl defaultSlotManager;

    public SlotsChangeNotification(DefaultSlotManagerImpl defaultSlotManager) {
        this.defaultSlotManager = defaultSlotManager;
    }

    @Override
    public void update(Observable source, Object message) {
        if (message instanceof Slots) {
            if (defaultSlotManager.notifierList == null || defaultSlotManager.notifierList.isEmpty()) {
                return;
            }
            defaultSlotManager.notifierList.forEach(notifier -> {
                try {
                    notifier.notifySlotsChange((Slots) message);
                } catch (Throwable th) {
                    logger.error("[notify] notifier [{}]", notifier.getClass().getSimpleName(), th);
                }
            });
        }
    }
}
